Welcome back. Let's take a snippet of code
that we've seen before and turn it into a function,
so we can invoke it at anytime instead of copying and editing the code.
We've seen something like this before,
we're trying to eventually get to defining a function total,
but here's some code that computes the total of a list,
the sum of all the values,
and it uses the accumulator pattern.
Here, we've got a particular list one, five and seven.
We start our accumulator with zero,
we iterate through the list each time updating the total.
The accumulator gets its old value plus the current number,
so the accumulator total starts at zero and it ends up being zero plus one for one,
one plus five makes six,
six plus seven makes 13 and we print out 13.
Now, the question is,
how can we make it work for any list of integers rather than just for one, five, seven?
You can see here that the problem that
we're being asked to do is define a function called total,
and so we're getting an error because total wasn't even defined.
We never created the function called total.
So, let's do that, let's define a function called total.
We'll put all of this code inside that function.
So, we're defining a function total.
It's going to have some parameters,
we're going to come back to that in a second,
and then all of this code we'll put inside here,
and we're going to have to make some adjustments.
The first thing is that we want total to take an input,
we don't want it to just work on this particular list of one, five and seven.
We want it to work on any list.
So, let's make that be
a formal parameter when the total function gets invoked that's when one,
five, seven or some other list will be specified.
So, we're going to have that behind the scenes
assignment to whatever list we want to run this
on rather than having the assignment to a particular value.
Then of course we need to, let's invoke total,
we're going to invoke it and assign the value to a variable called Y.
Let's see what happens in code lens with this.
So, we first get the variable total bound to the function,
we've defined the function,
and now we're invoking it on the list one, five, seven.
So, there's a behind the scenes invocation.
The variable LST now gets bound to the list one,
five, seven and we're ready to execute the lines of code.
We're going tot equal to zero,
and we're going to iterate num is first bound
one then it'll get bound to five, and then to seven.
This time is bound to one and tot gets updated to be zero plus one,
and num is five,
tot gets updated to be one plus five and so on.
Now, we're printing out that total,
and we're returning it.
Whoops! No we're not.
So, this is that gotcha that often gets people when they're new to writing
functions that we don't want to print out the total inside the function,
we want to return the total.
What will happen here is that Y is going to be bound to none,
and we really wanted Y to be bound to 13.
So, the way to fix that is instead of printing the total,
we're going to return the total.
So, before I do that, I just want to show you one other thing that's instructive.
I hide cold lens, if I run this,
I'm going to get errors because our function is not right,
but I'm also going to get a lot of outputs.
The reason is, and this can be confusing,
we have tests, when give you these exercise for example,
writing a function named total,
we add some tests where we behind the scenes are invoking
the function total and checking to make sure that it's giving the right output.
So, for example we're invoking it on the list one, two, three, four,
five and we're expecting the total to be 15.
One plus two plus three plus four plus five.
But the actual value that's getting returned is none,
and so we're getting an error.
But each time we invoke the function,
our line five is running,
and so we're actually printing out the 15.
The 13 is coming from our invocation.
But the 15 is coming from when there's
that behind the scenes invocation for the test then we have
zero when we invoke it on a list of
zeros and we have another zero and when we invoke it on an empty list,
and we have lists of just two.
It's printing out every time,
but it's returning the value none and so our tests are failing. Let's fix that.
Let's return tot, and return does not use parentheses it's statement not a function call.
Now, when we run it, we will pass the tests and we will
not print out anything here in the output window because we had no print statements.
If we wanted to print out Y,
we could get the value 13 to print.
The 13 prints, but we don't get any printouts from the rest of the tests,
all of those cause the code inside total to run,
but it doesn't have any print statements.
So, we don't get any confusing outputs there.
Let's look back at what we did.
We started with our accumulation snippet,
with just one hard-coded list,
and just to remind you.
So, we started with
just this hard-coded snippet that worked to find the total of a particular list.
Then we converted it to a function definition.
The list became a parameter
of the function rather than being hard-coded as a particular one.
The specific list that we were originally working
with became an input or an actual parameter.
It gets bound to the formal parameter.
Then we needed to change the print to return.
We needed to return the total,
and print it out here rather than doing a print inside the function.
This is a common process for abstracting
from a bit of code that works on a particular value,
you make that value be a formal parameter name of the function,
you pass specific values in when you invoke the function.
The challenge is to make the bit of code inside the function
be more general so that it works on any possible input.
In this case, any list of numbers. See you next time.